package com.derbysoft.defender.controller;

import com.derbysoft.defender.mp.api.request.UserParameter;
import com.derbysoft.defender.mp.api.requester.UserRequester;
import com.derbysoft.defender.mp.api.response.UserDetails;
import com.derbysoft.defender.mp.api.response.UserSummary;
import com.derbysoft.defender.util.JsonUtil;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>some description here.</p>
 * Created by Afee Lee on 2017-03-26 11:05
 *
 * @author Afee Lee
 * @version 1.0
 * @see Object
 * @since 1.8
 */
@RestController
@RequestMapping("/wechat/mp")
public class WeChatMpController extends AbstractController {

    @GetMapping(value = "/user/list")
    public Object getUserList() {
        UserRequester userRequester = new UserRequester(getAccessToken().getAccess_token());
        UserSummary summary = userRequester.listUsers();
        Map<String, Object> users = new HashMap<>();
        users.put("count", summary.getCount());
        users.put("total", summary.getTotal());

        List<String> ids = summary.getData().getOpenid();
        if (!CollectionUtils.isEmpty(ids)) {
            UserParameter parameter = new UserParameter();

            for (String id : ids) {
                parameter.addParam(new UserParameter.Param(id, UserRequester.LANG_DEFAULT));
            }

            UserDetails userDetails = userRequester.batchGetUsers(parameter);
            users.put("detail", userDetails);
        }

        return JsonUtil.toPrettyJson(users);
    }
}
